layui.use(["element", "layer", 'util', "jquery"], function (element, layer, util, $) {

    window.createObject = function (tarname, attrs, text) {
        var obj = $(tarname);
        if (attrs) {
            obj.attr(attrs);
        }
        if (text) {
            obj.text(text);
        }
        return obj;
    }

    var cid = sessionStorage.getItem("cid");
    if (cid == null || cid == "") {
        alert("请先登录！")
        location.href = "login.html";
        return
    }
    $.get("http://localhost:8080/c", {"customerid": cid}, function (rs) {
        if (rs.code != null) {
            layer.msg(rs.msg);
            return
        }
        $.each(rs.data, function (i, n) {
            var objA = createObject("<a/>", {"href": "javascript:", "lay-add-submenu": "subMenu", "pid": n.per_id,"class": 'layui-icon '+n.icon, "style": 'font-size: 15px'}, n.per_name);
            var objLi = createObject("<li/>", {"class": "layui-nav-item"}, null);
            var objDL = createObject("<dl/>", {"class": "layui-nav-child", "id": "D" + n.per_id})
            objLi.append(objA)
                .append(objDL)
                .appendTo("#lmenu");
        });
        element.render('nav', 'leftMenu');
    });

    util.event('lay-add-submenu', {
        subMenu: function (othis) {
            var parentid = $(othis).attr("pid");
            $("#D" + parentid).empty();
            $.get("http://localhost:8080/sc", {"parentid": parentid}, function (rs) {
                $.each(rs.data, function (i, m) {
                    var objA = createObject("<a/>", {"href": m.per_url, "target": "ifrmContainer", "class": 'layui-icon ' + m.icon, "style": 'font-size: 15px'}, m.per_name);
                    var objDD = createObject("<dd/>")
                    objDD.append(objA)
                        .appendTo("#D" + parentid);
                });
                element.render('nav', 'leftMenu');
            });
        }
    });
});